package com.sdry.web.controller.zc.quality;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcEvaluateCriterionEntity;
import com.sdry.model.zc.ZcEvaluateEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.zc.ZcCriterionService;
import com.sdry.service.zc.ZcEvaluateService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcEvaluateController   
 * @Description:  组件评价
 * @Author:       zc   
 * @CreateDate:   2018年10月9日 下午2:07:50   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("evaluate")
public class ZcEvaluateController {

	@Resource
	private ZcCriterionService zcCriterionService;
	@Resource
	private ZcEvaluateService zcEvaluateService;
	/**
	 * 进入组件评价页面
	 * @return
	 */
	@RequestMapping("enterEvaluatePage")
	public String enterEvaluatePage(Model model){
		ZcEvaluateCriterionEntity zcEvaluateCriterionEntity = zcCriterionService.selectEvaluateCriterion();
		model.addAttribute("zcEvaluateCriterionEntity", zcEvaluateCriterionEntity);
		return "/zc/quality/evaluate";
	}
	/**
	 * 保存组件评价
	 * @param request
	 * @param response
	 * @param zcEvaluateEntity
	 */
	@RequestMapping("insertEvaluate")
	public void insertEvaluate(HttpServletRequest request,HttpServletResponse response,ZcEvaluateEntity zcEvaluateEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcEvaluateEntity.setTestPerson(userName);
		int affact = zcEvaluateService.insertEvaluate(zcEvaluateEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查看今日组件评价记录
	 * @param request
	 * @param response
	 * @param zcEvaluateEntity
	 * @param httpSession
	 */
	@RequestMapping("selectTodayLogList")
	public void selectTodayLogList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		Date date = new Date();
		zcQueryCriteria.setWorker(userName);
		zcQueryCriteria.setWorkTime(date);
		List<ZcEvaluateEntity> todayLogList = zcEvaluateService.selectTodayLogList(zcQueryCriteria);
		int count = zcEvaluateService.countTodayLogList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(todayLogList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
